<?php

namespace Biz\service\Album;


use Biz\model\Album\AlbumCategoryModel;
use Biz\model\Album\AlbumCategoryRelateModel;
use Biz\model\Album\AlbumRelateTagsModel;

class AlbumCategoryService
{
    /**
     * @param $data
     * @param $categoryId
     * @return bool
     */
    public static function saveAlbumCategory($data, $categoryId): bool
    {

        if ($categoryId > 0) {
            //更新分类标签关系
            AlbumCategoryModel::updateDataByWhere($data, ['id' => $categoryId]);
        } else {
            //新添加的分类 默认为隐藏
            $categoryId = AlbumCategoryModel::insertData(array_merge($data,
                ['status' => AlbumCategoryModel::STATUS_HIDE]));
        }

        //分类关联清单
        $tagStr = $data['tag_name'] ?? '';
        $result = self::saveAlbumCategoryTag($tagStr, $categoryId);
        if ($result) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * @param $tagStr
     * @param $categoryId
     * @return bool
     */
    public static function saveAlbumCategoryTag($tagStr, $categoryId): bool
    {
        if ($tagStr) {
            $tagArr = explode(',', $tagStr);
            $albumIds = array_unique(AlbumRelateTagsModel::selectByWhere([
                'tag_name' => $tagArr,
                'status' => AlbumRelateTagsModel::STATUS_NORMAL,
            ], 'album_id'));
            AlbumCategoryRelateModel::updateDataByWhere(
                ['status' => AlbumCategoryRelateModel::STATUS_DELETE],
                ['category_id' => $categoryId]
            );
            $insertData = [];
            foreach ($albumIds as $albumId) {
                $upCount = AlbumCategoryRelateModel::updateByWhere(['status' => AlbumCategoryRelateModel::STATUS_NORMAL],
                    ['category_id' => $categoryId, 'album_id' => $albumId]);
                if ($upCount == 0) { //需要插入的数据 先放到 $insertData 数组
                    $time = date('Y-m-d H:i:s');
                    $insertData [] = [
                        'category_id' => $categoryId,
                        'album_id' => $albumId,
                        'addtime' => $time,
                        'edittime' => $time,
                    ];
                }
                if (count($insertData) == 100) { //设置批量插入数据的个数限制
                    AlbumCategoryRelateModel::InsertData($insertData);
                    $insertData = []; //把 $insertData  重新置为空数组，等待下次数据插入
                }

            }
            if ($insertData) { //兜底 把未满足100个的insert
                AlbumCategoryRelateModel::InsertData($insertData);
            }

        }else{
            AlbumCategoryRelateModel::updateDataByWhere(
                ['status' => AlbumCategoryRelateModel::STATUS_DELETE],
                ['category_id' => $categoryId]
            );
        }

        return true;
    }

    /**
     * @param $albumId
     * @return bool
     */
    public static function saveAlbumToCategory($albumId): bool
    {
        //查询清单关联的标签
        $tagNameArr = array_unique(AlbumRelateTagsModel::selectByWhere([
            'album_id' => $albumId,
            'status' => AlbumRelateTagsModel::STATUS_NORMAL,
        ], 'tag_name'));
        //查询所有的分类列表
        $category = AlbumCategoryModel::selectByWhere(['status[!]' => AlbumCategoryModel::STATUS_DELETE]);
        $categoryIds = [];
        if ($category) {
            foreach ($category as $item) {
                //取出分类列表里面的 tag_name集合
                $tag = explode(',', $item['tag_name']) ?? [];
                if ($tag) {
                    foreach ($tag as $tagValue) {
                        if (in_array($tagValue, $tagNameArr)) {
                            $categoryId = $item['id'];
                            $upCount = AlbumCategoryRelateModel::updateByWhere(['status' => AlbumCategoryRelateModel::STATUS_NORMAL],
                                ['category_id' => $categoryId, 'album_id' => $albumId]);
                            if ($upCount == 0) {
                                $time = date('Y-m-d H:i:s');
                                $insertData = [
                                    'category_id' => $categoryId,
                                    'album_id' => $albumId,
                                    'addtime' => $time,
                                    'edittime' => $time,
                                ];
                                AlbumCategoryRelateModel::InsertData($insertData);
                            }
                            $categoryIds[] = $categoryId;
                        }
                    }
                }
            }
            //更新 如果不在这个分类里面的  则取消关系
            if (!empty($categoryIds)) {
                AlbumCategoryRelateModel::updateDataByWhere(
                    ['status' => AlbumCategoryRelateModel::STATUS_DELETE],
                    ['category_id[!]' => $categoryIds, 'album_id' => $albumId]
                );
            } else {
                AlbumCategoryRelateModel::updateDataByWhere(
                    ['status' => AlbumCategoryRelateModel::STATUS_DELETE],
                    ['album_id' => $albumId]
                );
            }
        }

        return true;
    }

}